<?
/*佣金的添加修改和读取
@method send 添加 @param  设置的值 梯度Commission 比例Proportion 会员ID fid
@method get   设置页面读取当前设置   返回值 :target 梯度    radio 比例
@method got    读取 @param  当前佣金值 Commission 会员ID fid  可以自动得出赢得的返利比例
@table pft_member_extinfo
*/
// exit;
include("../../com.inc.php");

$fid = $_REQUEST['fid'];
// print_r($_REQUEST);exit;
$method = $_REQUEST['method'];
switch($_REQUEST['method']){
    case 'send':
        //佣金 和 比例
        $Commission = $_REQUEST['Commission'];
        $value = 0;
        foreach($Commission as $k1 => $v){
            if(($v+0)<=0){print_r(json_encode('请在佣金梯度中输入正数'));exit;}
            if($v<=$value){print_r(json_encode('请从小到大输入佣金梯度'));exit;}
            $value = $v;
        }
        $Proportion = $_REQUEST['Proportion'];
        $value = 0;
         foreach($Proportion as $k2 => $v){
            if(($v+0)<=0){print_r(json_encode('请在返利比例中输入正数'));exit;}
            if($v<=$value){print_r(json_encode('请从小到大输入返利比例'));exit;}
            $value = $v;
        }
        if($k2!==$k1){print_r(json_encode('请设置相同个数的佣金梯度和返利比例'));exit;}

        $commiss = implode("|",$Commission);
        $propor    = implode("|",$Proportion);
        $sql = "UPDATE pft_member_extinfo set Commission='$commiss',Proportion='$propor' where fid=$fid";
        $GLOBALS['le']->query($sql);
        print_r(json_encode(array('code'=>200)));
		
        break;
	case 'get':
		$sql= "SELECT Commission,Proportion from pft_member_extinfo where fid=$fid";
        $GLOBALS['le']->query($sql);
        $row = $GLOBALS['le']->fetch_assoc();
		
        $commiss = explode("|",$row['Commission']);
        $propor    = explode("|",$row['Proportion']);
		
		$arr = array();
		foreach($commiss as $k => $v){
			$arr[$k] = array('Commission'=>$v,'Proportion'=>$propor[$k]); 
		}
		
		$brr = json_encode($arr);
		print_r($brr);		
		break;
    case 'got':
        $Commission = $_REQUEST['Commission'];

        $sql= "SELECT Commission,Proportion from pft_member_extinfo where fid=$fid";
        $GLOBALS['le']->query($sql);
        $row = $GLOBALS['le']->fetch_assoc();
		
        $commiss = explode("|",$row['Commission']);
        $propor    = explode("|",$row['Proportion']);

        $Gradient = 0;
        //数组倒序排列
        $arr = array_reverse($commiss);
        $brr = array_reverse($propor);
        foreach ($arr as $key => $value) {
            if($Commission>($value*10000)){
               $Gradient = $Commission*$propor[$key]/100;
               break;
           }
           echo "悲剧啊连最低标准都没达到";
           break 2;
        }
        echo "您的佣金等级为$value,您的返利值为$Gradient.";
        break;
    default:
        exit("非法操作!");
        break;

}